package com.pengxun.manager.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.pengxun.manager.dao.system.RoleMenuDao;
import com.pengxun.manager.enums.PermissionTypeEnum;
import com.pengxun.manager.model.system.Menu;
import com.pengxun.manager.model.system.RoleMenu;
import com.pengxun.manager.model.system.UserRole;
import com.pengxun.manager.service.system.MenuService;
import com.pengxun.manager.service.system.RoleMenuService;
import com.pengxun.manager.service.system.UserRoleService;
import com.pengxun.manager.vo.MenuCodeTypeVO;
import com.pengxun.manager.vo.MenusVO;
import com.pengxun.manager.vo.RolePermissionVO;
import com.pengxun.manager.vo.UserPermissionVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.jdbc.page.Page;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {
	
	Logger logger = LoggerFactory.getLogger(RoleMenuServiceImpl.class);

	@Resource
	private RoleMenuDao roleMenuDao;

	@Resource
	private UserRoleService userRoleService;

	@Resource
	private MenuService menuService;

	@Override
	public int insert(RoleMenu roleMenu) {
		return roleMenuDao.insert(roleMenu);
	}

	@Override
	public int update(RoleMenu roleMenu) {
		return roleMenuDao.update(roleMenu);
	}

	@Override
	public int delete(Integer id) {
		return roleMenuDao.delete(id);
	}

	@Override
	public Page findByPage(Page page) {
		List<RoleMenu> roleMenus = roleMenuDao.findByPage(page);
		page.setData(roleMenus);
		return page;
	}

	@Override
	public List<RoleMenu> findByRoleCode(String roleCode, Integer status) {
		return roleMenuDao.findByRoleCode(roleCode, status);
	}

	@Override
	public List<RoleMenu> findByRoleCode(String roleCode) {
		return roleMenuDao.findByRoleCode(roleCode, null);
	}

	@Override
	public RoleMenu findByRoleAndPermission(String roleCode,
			String permissionCode) {
		return roleMenuDao.findByRoleAndPermission(roleCode, permissionCode);
	}

	@Override
	public void checkUserPermission(Long userId, String permissionCode) {
		RoleMenu roleMenu = null;
		// 获取用户角色
		List<UserRole> roleList = userRoleService.findByUserId(userId);
		// 校验用户所有角色是否有当前权限
		for (UserRole userRole : roleList) {
			roleMenu = findByRoleAndPermission(userRole.getRoleCode(),
					permissionCode);
			if (null != roleMenu) {
				break;
			}
		}
		// 返回结果
		if (null == roleMenu) {
			throw new EastrocException("没有访问权限");
		}
	}

	/**
	 * 获取用户所有权限 TODO 优化查询 优化逻辑
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public UserPermissionVO getUserPermission(Long userId) {
		if (null == userId) {
			return null;
		}
		// 获取用户角色
		List<UserRole> roleList = userRoleService.findByUserId(userId);
		if (null == roleList) {
			return null;
		}
		// 查询所有角色的菜单
		List<RoleMenu> roleMenuList = new ArrayList<RoleMenu>();
		List<RoleMenu> roleMenus = null;
		for (UserRole userRole : roleList) {
			roleMenus = findByRoleCode(userRole.getRoleCode(),
					PermissionTypeEnum.MENU.getType());
			if (null != roleMenus && roleMenus.size() > 0) {
				roleMenuList.addAll(roleMenus);
			}
		}
		if (roleMenuList.size() < 1) {
			return null;
		}
		// 菜单code集合
		List<String> menuCodes = new ArrayList<String>();
		// 菜单上下级关系
		for (RoleMenu roleMenu : roleMenuList) {
			menuCodes.add(roleMenu.getMenuCode());
		}
		// 用户所有菜单
		List<Menu> list = menuService.findByMenuCodes(menuCodes);
		if (null == list) {
			return null;
		}
		List<MenusVO> menusVOList = new ArrayList<MenusVO>();
		MenusVO chanVO = null;
		MenusVO menusVO = null;
		
		MenusVO chan2VO = null;
		// 组装菜单父子结构 TODO 多级结构递归改造
		for (Menu menu : list) {
			if (menu.getMenuType() == 0) {
				// 一级菜单
				menusVO = new MenusVO();
				menusVO.setIcon(menu.getMenuIcon());
				menusVO.setName(menu.getMenuName());
				menusVO.setPath(menu.getPageUrl());
				List<MenusVO> chans = new ArrayList<MenusVO>();
				for (Menu menu2 : list) {
					if (menu2.getParentMenuCode().equals(menu.getMenuCode())) {
						// 子类
						chanVO = new MenusVO();
						chanVO.setIcon(menu2.getMenuIcon());
						chanVO.setName(menu2.getMenuName());
						chanVO.setPath(menu2.getPageUrl());
						//写死三层
						List<MenusVO> chans2 = new ArrayList<MenusVO>();
						for (Menu menu3 : list) {
							if(menu3.getParentMenuCode().equals(menu2.getMenuCode())){
								//menu2子类
								chan2VO = new MenusVO();
								chan2VO.setIcon(menu3.getMenuIcon());
								chan2VO.setName(menu3.getMenuName());
								chan2VO.setPath(menu3.getPageUrl());
								chans2.add(chan2VO);
							}
						}
						chanVO.setChildren(chans2);
						chans.add(chanVO);
					}
				}
				menusVO.setChildren(chans);
				menusVOList.add(menusVO);
			}
		}
		// 用户所有权限
		List<String> perList = new ArrayList<String>();
		roleMenuList = new ArrayList<RoleMenu>();
		for (UserRole userRole : roleList) {
			roleMenus = findByRoleCode(userRole.getRoleCode(),
					PermissionTypeEnum.PERMISSION.getType());
			if (null != roleMenus && roleMenus.size() > 0) {
				roleMenuList.addAll(roleMenus);
			}
		}
		for (RoleMenu string : roleMenuList) {
			perList.add(string.getPermissionCode());
		}
		// 返回用户权限对象
		UserPermissionVO userPermissionVO = new UserPermissionVO();
		userPermissionVO.setMenus(menusVOList);
		userPermissionVO.setPermissions(perList);
		return userPermissionVO;
	}

	/**
	 * 获取角色所有权限
	 */
	@Override
	public RolePermissionVO getRolePermission(String roleCode) {
		RolePermissionVO rolePermissionVO = new RolePermissionVO();
		List<RoleMenu> roleMenuList = findByRoleCode(roleCode);
		List<MenuCodeTypeVO> permissions = new ArrayList<MenuCodeTypeVO>();
		MenuCodeTypeVO menuCodeTypeVO = null;
		for (RoleMenu roleMenu : roleMenuList) {
			menuCodeTypeVO = new MenuCodeTypeVO();
			menuCodeTypeVO.setCode(roleMenu.getMenuCode());
			menuCodeTypeVO.setPerssionCode(roleMenu.getPermissionCode());
			menuCodeTypeVO.setType(roleMenu.getIsMenuPermission());
			permissions.add(menuCodeTypeVO);
		}
		rolePermissionVO.setPermissions(permissions);
		rolePermissionVO.setRoleCode(roleCode);
		return rolePermissionVO;
	};

	/**
	 * 修改用户权限
	 * 
	 * @param rolePermission
	 * @return
	 */
	public void updateRolePermission(RolePermissionVO rolePermission) {

		// 解析 权限 菜单集合
		List<RoleMenu> roleMenus = new ArrayList<RoleMenu>();
		RoleMenu roleMenu = null;
		for (MenuCodeTypeVO menuCodeTypeVO : rolePermission.getPermissions()) {
			roleMenu = new RoleMenu();
			if (menuCodeTypeVO.getType() == 0) {
				roleMenu.setMenuCode(menuCodeTypeVO.getCode());
			} else if (menuCodeTypeVO.getType() == 1) {
				roleMenu.setMenuCode(menuCodeTypeVO.getCode());
				roleMenu.setPermissionCode(menuCodeTypeVO.getPerssionCode());
			}
			roleMenu.setIsMenuPermission(menuCodeTypeVO.getType());
			roleMenu.setRoleCode(rolePermission.getRoleCode());
			roleMenus.add(roleMenu);
		}
		// 删除原权限
		deleteByRoleCode(rolePermission.getRoleCode());
		// 新增新分配权限
		batchInsert(roleMenus);
	}

	@Override
	public int deleteByRoleCode(String roleCode) {
		return roleMenuDao.deleteByRoleCode(roleCode);
	}

	/**
	 * 批量插入
	 * 
	 * @param roleMenus
	 * @return
	 */
	@Override
	public int batchInsert(List<RoleMenu> roleMenus){
		return roleMenuDao.batchInsert(roleMenus);
	}

	@Override
	public int deleteByMenuCode(String menuCode) {
		return roleMenuDao.deleteByMenuCode(menuCode);
	}

	@Override
	public List<RoleMenu> findByMenuCode(String menuCode) {
		return roleMenuDao.findByMenuCode(menuCode);
	}

	@Override
	public int deleteByPermissionCode(String permissionCode) {
		return roleMenuDao.deleteByPermissionCode(permissionCode);
	}
}
